Get `n` random values between 2 numbers having average `x`

Posted by Somnath Muluk on Stack Overflow See other posts from Stack Overflow or by Somnath Muluk
Published on 2012-03-30T13:07:56Z Indexed on 2012/03/30 17:29 UTC
Read the original article Hit count: 223

Filed under:
|
|
|

I want to get n random numbers(e.g n=16)(whole numbers) between 1 to 5(including both) so that average is x.

x can be any value between (1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5).

I am using PHP.

e.g. Suppose I have average x= 3.

Then required 16 whole numbers between 1 to 5(including both). like (1,5,3,3,3,3,2,4,2,4,1,5,1,5,3,3)

Update:

if x=3.5 means average of 16 numbers should be between 3.5 to 4.
and if x=4 means average of 16 numbers should be between 4 to 4.5
and if x=5 means all numbers are 5

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about random